<template>
  <div>
    <!-- 放一个cell显示当前的评论 -->
    <van-cell>
      <template>
        <div class="cmt-box">
          <van-image round fit="cover" :src="currentCmt.aut_photo"></van-image>
          <div class="info">
            <div>{{ currentCmt.aut_name }}</div>
            <div>{{ currentCmt.content }}</div>
            <div>
              <span class="time">{{ currentCmt.pubdate | relvTime }}</span>
            </div>
          </div>
        </div>
      </template>
    </van-cell>
    <!-- 分割线 -->
    <van-divider>回复列表</van-divider>

    <!-- list显示回复列表 -->
    <van-list
      class="cmt-list"
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell v-for="item in list" :key="item.com_id">
        <template>
          <div class="cmt-box">
            <van-image round fit="cover" :src="item.aut_photo"></van-image>
            <div class="info">
              <div>{{ item.aut_name }}</div>
              <div>{{ item.content }}</div>
              <div>
                <span class="time">{{ item.pubdate | relvTime }}</span>
              </div>
            </div>
          </div>
        </template>
      </van-cell>
    </van-list>

    <!-- 输入区域 -->
    <div class="send-box">
      <van-field v-model="cmt" center clearable placeholder="请输入评论内容">
        <template #button>
          <van-button @click="send" size="small" type="info">发布</van-button>
        </template>
      </van-field>
    </div>
  </div>
</template>

<script>
import { cmtListAPI, sendCmtAPI } from "@/api/comment";

export default {
  props: {
    currentCmt: {
      type: Object,
      required: true,
    },
  },

  data() {
    return {
      list: [],
      loading: false,
      finished: false,
      // 跟发表的输入框双向绑定
      cmt: "",
      offset: null,
    };
  },

  methods: {
    async onLoad() {
      const res = await cmtListAPI({
        type: "c",
        source: this.currentCmt.com_id,
        offset: this.offset,
      });

      // 把本次返回的last_id作为offset方便下次传递
      this.offset = res.data.last_id;
      this.list.push(...res.data.results);

      // 加载完数据把loading改为false
      this.loading = false;

      // 如果结束id等于本次的最后一条id，就代表本次请求后加载完了
      if (res.data.end_id == res.data.last_id) {
        this.finished = true;
      }
    },

    // 发布的点击事件
    async send() {
      // 非空判断
      if (this.cmt == "") return this.$toast("请输入点内容");

      // 能来到这证明不为空
      const res = await sendCmtAPI({
        target: this.currentCmt.com_id,
        content: this.cmt,
        art_id: this.$route.query.id,
      });

      // 把发表成功后服务器返回的评论的数据加到数组最前面即可
      this.list.unshift(res.data.new_obj);

      // 弹出成功提示
      this.$toast.success("发表成功");
      // 让当前评论的回复数量+1
      this.currentCmt.reply_count++

      // 清空输入框内容
      this.cmt = "";
    },
  },
};
</script>

<style lang="less" scoped>
.cmt-box {
  display: flex;
  .van-image {
    width: 40px;
    height: 40px;
  }

  .info {
    flex: 1;
    margin: 0 15px;

    .time {
      color: gray;
      font-size: 12px;
    }
    .van-button {
      margin-left: 5px;
      vertical-align: middle;
    }
  }
}

.send-box {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
}
</style>